<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

require_once(ROOT_PATH."includes/glossary.inc.php");

if ($param['op']=="save") {
	$gl = new glossary;
	$gl->english_term=$param['eterm'];
	$gl->translation=$param['translation'];
	$gl->context="";
	$gl->related=$param['related'];
	$gl->comments=$param['comments'];
	$gl->teamid = $param['teamid']!="" ? $param['teamid'] : getTeamId($CURRENT_USER);
	$gl->userid=$CURRENT_USER->userid;

	if ($param['id']!="") {
		$old = new glossary;
		$old->id=$param['id'];
		$loader->get($old);

		if ($old->english_term!=$gl->english_term and $loader->getBy($gl,$gl->findByTerm())!==false) {
			failed(s("The term already exists. If you need to add more possible translations edit the existing one."));
		} else {
			$gl->id=$param['id'];
			$loader->update($gl);
			mailGlossaryUpdate($CURRENT_USER,$gl,$old);
			success();
		}

	} else {
		if ($loader->getBy($gl,$gl->findByTerm())===false) {
			$loader->save($gl);
			success();
		} else {
			failed(s("The term already exists. If you need to add more possible translations edit the existing one."));
		}		
	}
} elseif ($param['op']=="edit") {
	$gl = new glossary;
	$gl->id=$param['id'];
	$ret = $loader->get($gl);
	if ($ret===false) {
		$gl->english_term=$param['id'];
		$gl->id="";
	}
	include(ROOT_PATH."/form/glossary_form.php");	
} elseif ($param['op']=="new") {
	include(ROOT_PATH."/form/glossary_form.php");	
} elseif ($param['op']=="jump") {
	$gl = new glossary;
	$gl->teamid = $param['teamid']!="" ? $param['teamid'] : getTeamId($CURRENT_USER);
	if ($param['id']=="ALL") {
		$list = $loader->loadList($gl);	
	} else {
		$list = $loader->loadListBy($gl,$gl->listStartsWith($param['id']));
	}
	out(jsonList($list,array("id","#english_term","related","#comments","lastupdate","userid","@translationlist")));
} elseif ($param['op']=="search") {
	if ($param['id']!="SR") {
		switch ($param['mode'] ) {
			case "opens":
				$list = searchOpen($param);
				break;
			case "all":
				$list = searchLocal($param);
				$list = array_merge($list,searchOpen($param));
				break;
			default:
				$list = searchLocal($param);
				break;
		}
		out(jsonList($list,array("id","#english_term","related","#comments","lastupdate","userid","@translationlist")));		
	}
}

?>